<template>
  <div class="chat-box">
    <div id="head">  <el-button type="info" size="medium " @click="unlogin">退出</el-button></div>
	  <div  class="header"> <p id="header"></p></div>
	  <div class="top">
		<div class="toper">
		<textarea class="ui" id="ii"></textarea>
		</div>
	  </div>
	  <div class="bottom">
		<div class="down">
		<textarea  class="ui" ref="sendMsg" v-model="contentText"></textarea>
		  <button ref="btn" @click="sendText">发送</button>
		</div>
	  </div>
	</div>
  </template>
  <script>
  export default {
	data () {
	  return {
		ws: null,
		count: 0,
      role: null,
		userId: null,
      copy:0,
      name:null,
		list: [],
		contentText: '' // input输入的值
	  }
	},
	created () {
    const name = Math.floor(Math.random() * (100000000 - 10000000)) + 10000000;
    this.name = name;
    this.getUserId()
	},
	mounted () {
	  this.initWebsocket()
	  this.$refs.sendMsg.focus()
    this.$nextTick(() =>{
      if (this.$route.query.role!=='0'){
      document.getElementById("head").style.display="none"
      console.log("asadsa")
    }
    })
  },

	updated () {
	},
	methods: {
    count(){
      this.copy=this.count
    },
    unlogin(){
      this.$router.push({path:"/"})
    },
	  getUserId () {
		this.userId = this.$route.query.userId;
    this.role = this.$route.query.role;
    console.log(this.userId);
	  },

	  // 发送聊天消息
	  sendText () {
		let _this = this
		_this.$refs.sendMsg.focus()
		if (!_this.contentText) {
		  return false
		}

		if (_this.ws.readyState === 1) {
      console.log(this.role)
      if(this.role==='1'){
        console.log("管理员")

        _this.ws.send("（管理员）"+this.userId+": "+this.contentText)
      }else if (this.role==='0'){
        console.log("用户")

        _this.ws.send("（用户）"+this.userId+": "+this.contentText)
      }else {
        _this.ws.send("游客"+this.name+": "+this.contentText)

      }
		  // 调用WebSocket send()发送信息的方法
		}
  
		_this.contentText = ''
	  },
    initWebsocket: function () {
      let _this = this
      let count=0
      // 判断页面有没有存在websocket连接
      if (window.WebSocket) {
        let ws = new WebSocket('ws://localhost:7000/hello1')
        this.count=count
        _this.ws = ws
        console.log(ws)
        ws.onopen = function (e) {
          count += 1;
          this.count=count
          _this.ws.send(0)
          document.getElementById("header").innerHTML="聊天室人数:"+this.count
          console.log()
          console.log(this.count)
          console.log('服务器连接成功')
        }
        ws.onclose = function () {
          count -= 1;
          this.count=count
          document.getElementById("header").innerHTML="聊天室人数:"+this.count

          console.log('服务器连接关闭')
        }
        ws.onerror = function () {

          console.log('服务器连接出错')
        }
        ws.onmessage = function (e) {
          console.log(e.data)
          // 接收服务器返回的数据
          if (typeof (e.data) == "string") {
           count=e.data.charAt(e.data.length-1)
            console.log(e.data.substr(0,e.data.length-1))
            console.log(count)
            if (e.data.substr(0,e.data.length-1) === "[客户端]") {
              this.count=count
              document.getElementById("header").innerHTML="聊天室人数:"+this.count
              console.log(this.count)
              console.log(e.data)
            } else if (e.data.substr(0,e.data.length-1) === "[客户端]离开了") {
              this.count = count;
              document.getElementById("header").innerHTML="聊天室人数:"+this.count
              console.log(this.count)
            } else {
              console.log(e.data)
              // this.list.push.data(e.data);
              var rt = document.getElementById("ii");
              rt.value = rt.value + e.data;
            }
          }
        }
      }
    }
  },
	
	watch: {}
  }
  </script>
  <style scoped>
  #head{
    position: absolute;
    height: 10%;
    width: 100%;
    border-bottom: black;
    align-content: flex-start;
    bottom: 99%;
    z-index: 2;

    background-color: #2B333F;
  }
  .message {
	padding-top: 60px;
	margin-bottom: 60px;
	height: 100%;
	overflow: auto;
  }
  .ui{
    font-size: 24px;
    font-weight: bold;
    color: #091e4b;
  }
  .header {
	width: 100%;
	height: 50px;
    line-height: 50px;

    text-align: center;
	background: #0099FF;
	color: #fff;
	font-size: 18px;
	position: relative;
	top: 0;
	z-index: 3;
  }
  .bottom {
	position: relative;
	height: 30%;
	bottom: 0;
	width: 100%;
	border-top: 1px solid #ccc;
  }
  .down {
	display: flex;
	align-items: center;
	padding: 5px;
	background: #fff;
  }
  .top{
	position: relative;
	height: 70%;
	bottom: 0;
	width: 100%;
	border-top: 1px solid #ccc;
  }
  .toper{
	
	display: flex;
	align-items: center;
	padding: 5px;
	background: #fff;
  
  }
  .top textarea {
	position: relative;
	width:100%;
	height: 450px;
	font-display: 18px;
  }
  .bottom textarea {
	position: relative;
	height: 140px;
	width:100%;
	font-display: 18px;
  }
  .bottom button {
	border: none;
	width: 70px;
	height: 44px;
	background: blue;
	color: #fff;
	line-height: 40px;
	text-align: center;
	border-radius: 5px;
	margin-left: 10px;
  }
  .msg-box {
	display:flex;
	align-items: center;
	justify-content: flex-end;
	width: 100%;
	margin: 20px 10px 0px 0px;
  }
 
  .avtive {
	display: inline-block;
	background: blue;
	color: #fff;
	padding: 10px;
	font-size: 16px;
	border-radius: 6px;
	margin-right: 10px;
	flex-wrap: wrap;
	word-break: break-all;
	max-width: 220px;
  }
  .header-avtor {
	width: 40px !important;
	height: 40px;
	border-radius: 50%;
	background: #ccc;
  }
  .avtive-left {
	justify-content: flex-start;
  }
  .chat-box
{
	position: relative;
    height: 100%;
    width: 100%;
	background-color: #fff;
}  
</style>
  
  